<%--
  @author Duminda Dharmakeerthi
  @author Supun Nimesh Karunathilaka
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<script src="${pageContext.request.contextPath}/js/printer.js" type="text/javascript"></script>
<%--<link rel="stylesheet" href="${pageContext.request.contextPath}/css/printDetailForm.css" media="print"/>--%>

<script type="text/javascript">
    $(document).ready(function() {
        $('#deed-tree').treegrid({
            title:$('#longTermLeaseHistory').val(),
            nowrap: false,
            rownumbers: true,
            animate:true,
            collapsible:true,
            idField:'deed',
            treeField:'deed',
            frozenColumns:[
                [
                    {title:$('#leaseHistory').val(),field:'deed',width:172,
                        formatter:function(value) {
                            return '<span style="color:red">' + value + '</span>';
                        }
                    }
                ]
            ],
            columns:[
                [
                    //                    {field:'sublease',title:'Sub-Lease',width:62},
                    {field:'name',title:$('#reference').val(),width:430}
                ]
            ]
        });
        loadAlienationTree();
    });

    function loadAlienationTree() {
        var grantId = $('#grantId').attr('value');
        var parent;
        $.getJSON('${pageContext.request.contextPath}/common/AlienationHistoryLookupService', {grantId:grantId}, function(list) {
            var AList = list.alienationList;
            var i = AList.length - 1;
            for (i; i > -1; i--) {
                if (AList[i].immediateParent == null) {
                    parent = null;
                } else {
                    parent = $('#deed-tree').treegrid('find', getImmediateParentId(AList[i]));
                }

                var id = AList[i].id;
                var data = [
                    {
                        deed: id,
                        name: AList[i].referenceNo}
                ];
                addAlienation(parent, data);
            }
        });
    }

    function getImmediateParentId(obj) {
        var p;
        $.each(obj, function(key, element) {
            if (key == "immediateParent") {
                p = element.id;
            }
        });
        return p;
    }

    function addAlienation(parent, data) {
        if (parent != null) {
            parent.transfer = '';
            parent.sublease = '';
            $('#deed-tree').treegrid("refresh", parent.deed);
        }
        $('#deed-tree').treegrid('append', {
            parent: (parent ? parent.deed : null),
            data: data
        });
    }
</script>


<SCRIPT language="javascript">
    function addPaymentRow(tableID) {

        var table = document.getElementById(tableID);

        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);

        var str = '<td>' + $('#year').val() + '</td>' +
                '<td><input type="text" id="year' + rowCount + '" name="paymentHistory[' + rowCount + '].yearOfPayment"/></td>' +
                '<td>' + $('#amount').val() + '</td>' +
                '<td><input type="text" id="amount' + rowCount + '" name="paymentHistory[' + rowCount + '].amount"/></td>' +
                '<td><input type="checkbox"/></td>'

        row.innerHTML = str;

    }

    function deletePaymentRow(tableID) {
        try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for (var i = 0; i < rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[4].childNodes[0];
                if (null != chkbox && true == chkbox.checked) {
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }
            }
        } catch(e) {
            alert(e);
        }
    }

</SCRIPT>

<s:include value="../../printCaller.jsp"/>

<div id="printDivId" class="form-outer">
    <h2><s:label value="%{getText('detailed_land_info.label')}"/> - <s:label
            value="%{getText('long_term_lease.label')}"/></h2>
    <hr/>
    <s:if test="hasActionErrors() || hasActionMessages()">
        <div id="message-box" class="message-box">
            <div class="action-msg"><s:actionmessage/></div>
            <div class="action-err"><s:actionerror/></div>
        </div>
    </s:if>
    <div id="deedPrint">
        <table id="deed-tree" class="width-100">
            <%-- Deed tree will be here --%>
        </table>
    </div>
    <s:form id="gov-land-detail-info-form" method="POST">
        <div id="tabs">
            <%--<div id="printbtn" class="form-button-div right-align">
                    &lt;%&ndash;<input type="button" value="Print" id="print"  class="print-btn">&ndash;%&gt;
                <input type="button" value="Print" onclick="printGrantLongPage();">
                <input class="printButton" type="button" value="Back" onclick="goBack();">

            </div>--%>


            <div id="land-details">
                <h3><s:label value="%{getText('land_details.label')}"/></h3>
                <table class="width-100">

                    <s:include value="../landDetailsReport.jsp"/>

                </table>
                <hr/>
            </div>

            <div id="long-term-lease-details">
                <h3><s:label
                        value="%{getText('long_term_lease_details.label')}"/></h3>
                <table class="width-100">
                    <tr>
                        <td><s:label value="%{getText('lease_no.label')}"/></td>
                        <td><s:label name="alienation.referenceNo"/></td>
                        <td><s:label value="%{getText('date_of_approved.label')}"/></td>
                        <td><s:label name="alienation.issueDate"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('folio_no.label')}"/></td>
                        <td><s:label id="folioNo" name="alienation.folioNumber" cssClass="width-200-px"/></td>
                        <td><s:label value="%{getText('folio_date.label')}"/></td>
                        <td><s:label id="folioDate" name="alienation.folioDate" onclick="selectDate('folioDate')"
                                     cssClass="width-200-px"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('folio_location.label')}"/></td>
                        <td colspan="3"><s:label id="folioLocation" name="alienation.folioLocation" rows="1"
                                                 cssClass="width-595-px"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('lease_period.label')}"/></td>
                        <td><s:label name="alienation.leaseDetail.leasePeriod"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('lease_diagram_no.label')}"/></td>
                        <td colspan="3"><s:label name="alienation.leaseDetail.leaseDiagramNo"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('purpose_of_lease.label')}"/></td>
                        <td colspan="3"><s:property value="purposeOfLease"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('present_usage.label')}"/></td>
                        <td colspan="3"><s:property value="presentUsage"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('valuation_of_land.label')}"/></td>
                        <td><s:label value="%{getText('developed_value.label')}"/></td>
                        <td colspan="2"><s:label name="alienation.leaseDetail.developedValue"/></td>
                    </tr>
                    <tr>
                        <td rowspan="2"></td>
                        <td><s:label value="%{getText('commercial_value.label')}"/></td>
                        <td colspan="2"><s:label name="alienation.leaseDetail.commercialValue"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('undeveloped_value.label')}"/></td>
                        <td colspan="2"><s:label name="alienation.leaseDetail.unDevelopedValue"/></td>
                    </tr>
                    <tr>
                        <td><s:label value="%{getText('lease_arrears.label')}"/></td>
                        <td colspan="3"><s:label id="lease-arrears" cssClass="width-200-px"
                                                 name="alienation.leaseDetail.leaseArrears"/></td>
                    </tr>
                    <tr>

                        <td><s:label cssClass="amend" value="%{getText('rivision_interval.label')}"/></td>
                        <td><s:label cssClass="amend width-200-px" id="amend-period"
                                     name="alienation.leaseDetail.leaseAmendPeriod"/></td>
                    </tr>

                    <tr>
                        <td><%--<s:label value="%{getText('payment_history.label')}"/> --%></td>
                        <td colspan="3">
                            <table id="paymentTable">
                                    <%-- PaymentHistory table will be here --%>
                            </table>
                        </td>
                    </tr>

                    <s:hidden id="year" value="%{getText('year.label')}"/>
                    <s:hidden id="amount" value="%{getText('amount.label')}"/>
                    <tr>
                        <td><s:label value="%{getText('comment_about_alienation.label')}"/></td>
                        <td colspan="3"><s:label name="alienation.alienationComment" cssClass="width-595-px"/></td>
                    </tr>
                </table>
                <hr/>

            </div>

            <s:if test="alienation.mortgage != null">
                <h3><s:label value="%{getText('mortgage_details.label')}"/></h3>

                <div id="mortgage-details">
                    <table class="width-100">
                        <tr>
                            <td><s:label value="%{getText('name_of_bank_institute.label')}"/></td>
                            <td colspan="3"><s:label name="mortgage.nameOfBank"
                                                     cssClass="width-610-px"/></td>
                        </tr>
                        <tr>
                            <td><s:label value="%{getText('address_of_bank_institute.label')}"/></td>
                            <td colspan="3"><s:label name="mortgage.addressOfBank"
                                                     cssClass="width-610-px"/></td>
                        </tr>
                        <tr>
                            <td><s:label value="%{getText('started_date.label')}"/></td>
                            <td><s:label name="mortgage.startedDate"/></td>
                            <td><s:label value="%{getText('ending_date.label')}"/></td>
                            <td><s:label name="mortgage.endingDate"/></td>
                        </tr>
                        <tr>
                            <td><s:label value="%{getText('mortgage_conditions.label')}"/></td>
                            <td colspan="3"><s:label name="mortgage.conditions"
                                                     cssClass="width-610-px"/></td>
                        </tr>
                        <tr>
                            <td><s:label value="%{getText('power_of_attorney.label')}"/></td>
                            <td colspan="3"><s:label name="mortgage.powerOfAttorney"
                                                     cssClass="width-610-px"/></td>
                        </tr>
                    </table>
                    <hr/>
                </div>
                <s:hidden name="alienation.mortgage.id"/>
            </s:if>

            <div id="personal-details">
                <h3><s:label value="%{getText('holder_details.label')}"/></h3>
                <table class="width-100">
                    <s:include value="../personalDetailsReport.jsp"/>
                </table>
                <hr/>
                <s:hidden name="alienation.id"/>
                <s:hidden name="contact.id"/>
                <s:hidden name="holder.id"/>

            </div>
            <s:if test="land.occupiers.size() >0">
            <div id="occupier-details">
                <h3><s:label value="%{getText('occupier_details.label')}"/></h3>
                <table class="width-100">
                    <s:include value="../occupierDetailsReport.jsp"/>
                </table>
                <hr/>
            </div>

        </s:if>


        </div>
    </s:form>
</div>

<s:hidden id="pls_enter_reference_no" value="%{getText('pls_enter_reference_no')}"/>
<s:hidden id="pls_enter_name_of_the_land" value="%{getText('pls_enter_name_of_the_land')}"/>
<s:hidden id="pls_enter_plan_no" value="%{getText('pls_enter_plan_no')}"/>
<s:hidden id="pls_enter_lot_no" value="%{getText('pls_enter_lot_no')}"/>
<s:hidden id="pls_enter_valid_no" value="%{getText('pls_enter_valid_no')}"/>
<s:hidden id="pls_select_gn_division" value="%{getText('pls_select_gn_division')}"/>

<s:hidden id="grantId" name="grantId"/>
<s:hidden id="longTermLeaseHistory" value="%{getText('long_term_lease_history.label')}"/>
<s:hidden id="leaseHistory" value="%{getText('lease_history.label')}"/>
<s:hidden id="reference" value="%{getText('reference.label')}"/>
<s:hidden id="transfer" value="%{getText('transfer.label')}"/>
<s:hidden id="subLease" value="%{getText('subLease.label')}"/>